#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace MenuTest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        SpriteBatch spriteBatch;

        SpriteFont font;

        MenuItem[] menuItem = new MenuItem[5];

        int selectedItem = 0;

        KeyboardState oldState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }


        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                font = content.Load<SpriteFont>("SpriteFont1");
                spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

                oldState = Keyboard.GetState();

                Color baseColor = Color.White;
                Color selectedColor = Color.Red;

                menuItem[0] = new MenuItem("MenuItem1", "Menu Item 1", font, new Vector2(50f, 100f), baseColor, selectedColor, true);
                menuItem[1] = new MenuItem("MenuItem2", "Menu Item 2", font, new Vector2(50f, 150f), baseColor, selectedColor, false);
                menuItem[2] = new MenuItem("MenuItem3", "Menu Item 3", font, new Vector2(50f, 200f), baseColor, selectedColor, false);
                menuItem[3] = new MenuItem("MenuItem4", "Menu Item 4", font, new Vector2(50f, 250f), baseColor, selectedColor, false);
                menuItem[4] = new MenuItem("Exit", "Exit Game", font, new Vector2(50f, 300f), baseColor, selectedColor, false);
            }

            // TODO: Load any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                // TODO: Unload any ResourceManagementMode.Automatic content
                content.Unload();
            }

            // TODO: Unload any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            for (int i = 0; i < menuItem.Length; i++)
            {
                menuItem[i].Selected = false;
            }

            KeyboardState kb = Keyboard.GetState();

            if ((kb.IsKeyDown(Keys.Up)) && (oldState.IsKeyUp(Keys.Up)))
            {
                selectedItem -= 1;
                if (selectedItem == -1)
                {
                    selectedItem = menuItem.Length - 1;
                }
             }

            if ((kb.IsKeyDown(Keys.Down)) && (oldState.IsKeyUp(Keys.Down)))
            {
                selectedItem += 1;
                if (selectedItem == menuItem.Length)
                {
                    selectedItem = 0;
                }
            }

            if ((kb.IsKeyDown(Keys.Space)) && (oldState.IsKeyUp(Keys.Space)))
            {
                if (menuItem[selectedItem].Name == "Exit")
                {
                    Exit();
                }
            }
            
            menuItem[selectedItem].Selected = true;

            oldState = kb;

            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            for (int i = 0; i < menuItem.Length; i++)
            {
                menuItem[i].Draw(spriteBatch);
            }

            spriteBatch.DrawString(font, menuItem[selectedItem].Name + " selected.", new Vector2(50f, 525f), Color.White);
            spriteBatch.DrawString(font, "Press SPACE to attempt to activate the current menu item.", new Vector2(50f, 550f), Color.White);
   
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }

    public class MenuItem
    {
        private string _name = "";
        private string _text = "";
        private SpriteFont _font;
        private Vector2 _position = Vector2.Zero;
        private Color _baseColor;
        private Color _selectedColor;

        private bool _selected = false;

        public bool Selected
        {
            get { return _selected; }
            set { _selected = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public MenuItem(string name, string text, SpriteFont font, Vector2 position, Color baseColor, Color selectedColor, bool selected)
        {
            _name = name;
            _text = text;
            _font = font;
            _position = position;
            _baseColor = baseColor;
            _selectedColor = selectedColor;
            _selected = selected;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (_selected)
            {
                spriteBatch.DrawString(_font, _text, _position, _selectedColor);
            }
            else
            {
                spriteBatch.DrawString(_font, _text, _position, _baseColor);
            }
        }
    }
}
